package com.example.mynote.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.EditText;
import android.widget.TextClock;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.mynote.activity.AddNoteActivity;
import com.example.mynote.activity.EditActivity;
import com.example.mynote.R;
import com.example.mynote.base.BaseFragment;
import com.example.mynote.base.ReAdapter;
import com.example.mynote.entity.StickyNote;

import org.litepal.LitePal;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;

public class StickFragment extends BaseFragment {
    private EditText noteSearch;
    private RecyclerView noteRecycler;
    private AppCompatImageButton noteAdd;
    private int[] color = {R.color.c1, R.color.c2, R.color.c3, R.color.c4, R.color.c5, R.color.c6, R.color.c7};
    private TextClock detDay, detDate, detTime;
    private List<StickyNote> all;
    private ReAdapter<StickyNote> adapter;

    @Override
    public void onResume() {
        super.onResume();
        initData();
        adapter.notifyDataSetChanged();
    }

    @Override
    protected int Layout() {
        return R.layout.fragment_bq;
    }

    @Override
    protected void initView(View v) {

        noteRecycler = (RecyclerView) v.findViewById(R.id.note_recycler);
        noteAdd = (AppCompatImageButton) v.findViewById(R.id.note_add);
        detDay = (TextClock) v.findViewById(R.id.det_day);
        detDate = (TextClock) v.findViewById(R.id.det_date);
        detTime = (TextClock) v.findViewById(R.id.det_time);
    }

    @Override
    protected void initData() {

        noteAdd.setOnClickListener(v -> {
            intentTo(AddNoteActivity.class);
        });

        setData();
    }

    private void setData() {
        all = LitePal.findAll(StickyNote.class);
        noteRecycler.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        adapter = new ReAdapter<StickyNote>(all) {
            private TextView stickTime;
            private TextView stickContent;
            private View stickColor;

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.stick_item;
            }

            @Override
            public void convert(VH holder, StickyNote data, int position) {
                stickColor = (View) holder.itemView.findViewById(R.id.stick_color);
                stickContent = (TextView) holder.itemView.findViewById(R.id.stick_content);
                stickTime = (TextView) holder.itemView.findViewById(R.id.stick_time);

                stickColor.setBackgroundResource(color[new Random().nextInt(color.length)]);
                stickContent.setText(data.getContent());
                stickTime.setText(new SimpleDateFormat("·HH:mm", Locale.CHINA).format(data.getTime()));
            }
        };
        noteRecycler.setAdapter(adapter);
        noteRecycler.setItemAnimator(new DefaultItemAnimator());

        adapter.setOnItemClickListener(new ReAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                startActivity(new Intent(getActivity(), EditActivity.class).putExtra("content", all.get(position).getContent()));
            }

            @Override
            public void onItemLongClick(View view, int position) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("删除")
                        .setMessage("你确定要删除这条便签吗？")
                        .setPositiveButton("确定", (dialog, which) -> {
                            LitePal.deleteAll(StickyNote.class, "content = ? and time = ?", all.get(position).getContent(), "" + all.get(position).getTime());
                            setData();
                        }).setNegativeButton("取消", (dialog, which) -> dialog.cancel())
                        .show();
            }
        });
    }
}